//
//  EditView.m
//  Custom_Weibo
//
//  Created by 陈鸿 on 16/3/22.
//  Copyright © 2016年 Leo_Lei. All rights reserved.
//

#import "EditView.h"
#import "SettingViewController.h"
#import "BaseNavigationController.h"

#define kIconTag    33333

@interface EditView()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

@implementation EditView

- (void)awakeFromNib {
    [self setNeedsLayout];
}

#pragma mark - SHOW_ICONS
- (void)layoutSubviews {
    NSArray *imageArray = @[@"tabbar_compose_idea",
                            @"tabbar_compose_photo",
                            @"tabbar_compose_headlines",
                            @"tabbar_compose_lbs",
                            @"tabbar_compose_review",
                            @"tabbar_compose_more"];
    NSArray *nameArray = @[@"文字", @"照片/视频", @"头条文章", @"签到", @"点评", @"更多"];
    
    self.backgroundColor = kRGB(255, 255, 255, 0.8);
    
    //  Set the icons and labels
    for (NSInteger i = 0; i < 3; i++) {
        for (NSInteger j = 0; j < 2; j++) {
            //  1.Icons
            UIButton *iconView = [[UIButton alloc] init];
            iconView.frame = CGRectMake(kScreenWidth / 8 + (kScreenWidth / 4 + 10) * i ,
                                        kScreenHeight / 2 + (kScreenWidth / 4 + 10) * j,
                                        kScreenWidth / 5, kScreenWidth / 5);
            iconView.layer.cornerRadius = kScreenWidth / 10;
            
            //  2.Labels
            UILabel *labelView = [[UILabel alloc] init];
            labelView.frame = CGRectMake(iconView.left, iconView.bottom - 20,
                                         iconView.size.width, iconView.size.height);
            labelView.textAlignment = NSTextAlignmentCenter;
            labelView.font = [UIFont systemFontOfSize:13];
            
            
            NSString *imageItem = [[NSString alloc] init];
            NSString *nameItem = [[NSString alloc] init];
            if (j == 0) {
                imageItem = imageArray[i];
                nameItem = nameArray[i];
                iconView.tag = i + kIconTag;
            }else if (j == 1) {
                imageItem = imageArray[i + 3];
                nameItem = nameArray[i + 3];
                iconView.tag = i + kIconTag + 3;
            }
            [iconView setBackgroundImage:[UIImage imageNamed:imageItem] forState:UIControlStateNormal];
            [iconView addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            labelView.text = nameItem;
            
            [self addSubview:iconView];
            [self addSubview:labelView];
        }
    }
    [self Elements];
}

#pragma mark OTHER_ELEMENTS
- (void)Elements {
    UIImageView *titleView = [[UIImageView alloc] init];
    titleView.frame = CGRectMake(kScreenHeight / 8, kScreenWidth / 4,
                                 kScreenWidth / 2, kScreenHeight / 6);
    titleView.image = [UIImage imageNamed:@"compose_slogan"];
    titleView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:titleView];
}

#pragma mark - BUTTON_ACTION
- (void)buttonAction:(UIButton *)button {
    //  Send text.
    if (button.tag == kIconTag) {
        SettingViewController *setting = [[SettingViewController alloc] init];
        
        BaseNavigationController *baseNav = [[BaseNavigationController alloc]
                                             initWithRootViewController:setting];
        [self.belongingController.navigationController presentViewController:baseNav
                                                                    animated:YES
                                                                  completion:nil];
    }else if (button.tag == kIconTag + 1) {
        //  Enter album for selecting photos.
        [self showAlbum];
    }
}

- (void)showAlbum {
    UIImagePickerControllerSourceType sourceType;
    sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = sourceType;
    picker.delegate = self;
    [self.belongingController presentViewController:picker animated:YES completion:nil];
}

@end
